package com.example.rk.service.impl;
import com.example.rk.common.MyPageInfo;
import com.example.rk.common.SessionParameter;
import com.example.rk.entity.Admin;
import com.example.rk.entity.Job;
import com.example.rk.mapper.AdminMapper;
import com.example.rk.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class AdminServiceImpl implements AdminService {
@Autowired
private AdminMapper adminMapper;
    @Override
    public String doRegister(Admin admin) {

        Admin dbAdmin=adminMapper.findByName(admin.getName());
        if(dbAdmin!=null){
            return "exist";
        }
        adminMapper.create(admin);
        return "OK";
    }

    @Override
    public String doLogin(Admin admin,HttpSession session ) {
        Admin dbAdmin=adminMapper.findByNameAndPwd(admin.getName(),admin.getPwd());
        if(dbAdmin!=null) {
            session.setAttribute(SessionParameter.ADMIN_INFO,dbAdmin);
            return "ok";}
        else{return "error";}
    }

    @Override
    public void deleteById(int id) {
        adminMapper.deleteById(id);
    }

    @Override
    public String doAdd(Admin admin) {
        Job dbadmin=adminMapper.findadd(admin.getName(),admin.getType());
        if (dbadmin==null){
            adminMapper.create(admin);
            return "ok";
        }else{
            return "exist";
        }

    }

    @Override
    public Admin findById(int id) {
        return adminMapper.findByID(id);
    }

    @Override
    public String doEdit(Admin admin) {
        try {
            adminMapper.doEdit(admin);
            return "ok";
        }catch (Exception e){
            System.out.println("AdminServiceImpl.doEdit() ERROR:e="+e);
        }
        return "false";

    }
    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        adminMapper.findBySearch(name,type);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(name);
        myPageInfo.setSearch2(type);

        return myPageInfo;

    }

    @Override
    public Admin getAdmin(int id) {
        return adminMapper.getAdmin(id);
    }

    @Override
    public void setImageUrl(String absolutePath, int id) {
        adminMapper.setImageUrl(absolutePath,id);
    }

}
